home *** CD-ROM | disk | FTP | other *** search
/ JCSM Shareware Collection 1996 September / JCSM Shareware Collection (JCS Distribution) (September 1996).ISO / tutorial / trac.zip / LESSON4 < prev    next >
Text File  |  1990-02-09  |  18KB  |  361 lines

  1.  
  2.  
  3.  
  4.         
  5.                          ELEMENTARY COMPUTER PROGRAMMING
  6.                                     Lesson 4
  7.         
  8.                                  Copyright 1990
  9.                           Castle Oaks Computer Services
  10.                               Post Office Box 36082
  11.                            Indianapolis, IN 46236-0082
  12.         
  13.         In the previous lessons, the necessary information was given for 
  14.         writing a TRAC program, executing a TRAC program, and writing a TRAP 
  15.         program.  In this lesson, the information to assemble a TRAP program 
  16.         will be given.
  17.         
  18.         First, you must create the TRAP program according to the rules given 
  19.         in Lesson 3.  Then you will be ready to assemble the program.  The 
  20.         general command line format for using the assembler is:
  21.         
  22.         TRAP [source file] [object file]
  23.         
  24.         The parameters in brackets are optional.  If either one is omitted, 
  25.         the assembler will request the needed file descriptor.  A file 
  26.         descriptor may include a drive letter and subcatalogs (if required.)  
  27.         If the object file descriptor is the same as that of an already 
  28.         existing file, the new object file being created will replace the 
  29.         existing one.  There are no limitations on the file extensions for 
  30.         either the source file or object file; however, it is recommended 
  31.         that you use a file extension of 'TRA' for source files and 'OBJ' 
  32.         for object files.  As you accumulate files, these extensions will 
  33.         help you to identify the types of the files.
  34.         
  35.         The assembler is a two pass assembler.  On the first pass, all 
  36.         locations are assigned a numeric memory address and those with 
  37.         mnemonic names will be entered into a symbol table for later use.  
  38.         The symbol table can hold only 200 different symbols.  Since this is 
  39.         a large number no provision has been included to detect symbol table 
  40.         overflow.  Should you have too many symbols, you will probably "lock 
  41.         up" the computer.  After the copyright notice appears, the next 
  42.         thing that displays is an "End of Pass 1" message.
  43.         
  44.         On the second pass, mnemonic operation codes are assigned numeric 
  45.         values; and mnemonic or relative addresses are assigned their numer-
  46.         ical equivalents.  Each line of assembled code is displayed along 
  47.         with the source code that generated it.  During this pass, if any 
  48.         syntactical errors are discovered, an appropriate error message will 
  49.         be displayed, some action will be taken, and the assembly will 
  50.         continue.  An error message will appear before the line of code that 
  51.         contains the error.  At the end of pass 2, if any errors were de-
  52.         tected, the number of errors will be displayed.  Also during pass 2, 
  53.         the object file will be written.  An object file will be created 
  54.         even if the program contains errors.  Therefore, if you try to 
  55.         execute a program with syntactical errors the program may abort or 
  56.         something more serious may happen (like "crashing" the computer.)
  57.         
  58.         
  59.         
  60.         
  61.                                       IV-1
  62.  
  63.  
  64.         When an assembly has been completed successfully, the object program 
  65.         may be executed with TRAC using the methods outlined in Lesson 2.  
  66.         (Note: Even if a program does not have any syntactical errors, it 
  67.         may have logical errors.  There is no way that an assembler can 
  68.         detect your logical errors.  It is strongly recommended that you 
  69.         manually perform one or more test cases for any programs you 
  70.         create.)
  71.         
  72.         In Lesson 3, an example program was shown in its source form (Figure 
  73.         III-8) and the resulting object code (Figure III-8a).  Note that the 
  74.         source code is carried over into the object file and when the pro-
  75.         gram is executed, everything beyond the first 15 columns is treated 
  76.         as a comment.
  77.         
  78.         In lesson 1, a flow chart (Figure I-4) and TRAC code (Figure I-5) 
  79.         were given of a method for converting an octal number to decimal.  
  80.         The same program is given here in Figure IV-1 and is included in the 
  81.         software package as FIGIV-1.TRA (see next page).  Note that the code 
  82.         generated by the assembler is identical to that that was hand coded 
  83.         for Figure I-5.  This resulted because care was taken in re-origin-
  84.         ing various parts of the program to force this to happen.  There was 
  85.         no necessity for doing so except to illustrate that we could force 
  86.         it to happen.  You are encouraged to take this source code and make 
  87.         other memory assignments, even rearrange the code to see how the 
  88.         memory assignments change or not.  Although you have considerable 
  89.         latitude in making assignments there are some limitations.  Note 
  90.         that in the program, where  relative addressing was used, the actual 
  91.         number, 2, had to be used whereas elsewhere the index register was 
  92.         referred to by the mnemonic name, I.  Therefore, it was necessary 
  93.         that I be made equivalent to 2.  Also the definition for the read 
  94.         area was placed last.  This was done so that we didn't have to 
  95.         explicitly reserve locations 1002, 1003, and 1004.  The re-origining 
  96.         at 0999 could be omitted and the program would function as before 
  97.         but the read area would start at location 1506 instead of 1000.  Try 
  98.         this, execute the resulting program and note that it will function 
  99.         the same.  Another possible modification would be to move the two 
  100.         lines,
  101.         
  102.         0001          0
  103.            I          0
  104.         
  105.         so they are the first two lines of the code and then you could omit 
  106.         the re-origin lines 0009, 1499, and 0999.  This would cause the 
  107.         object program to use consecutive locations.  STRT would now be 
  108.         location 0003, the actual code would occupy through location 0041, 
  109.         the constants and variables would use 0042 through 47, and the read 
  110.         area would start at location 0048.
  111.         
  112.         
  113.         
  114.         
  115.         
  116.         
  117.         
  118.         
  119.         
  120.         
  121.                                       IV-2
  122.  
  123.  
  124.         -----------------------------------------------------------------
  125.         0009          0 Origin
  126.         STRT     RD   X Read X (Also sets next 4 locations to zero)
  127.                  LD   X Load accumulator with X
  128.                  BZSTOP If X is zero jump to halt instruction
  129.                  BN*+4  If X is negative, skip next 3 instructions     
  130.                  LD ONE Load +1                                        
  131.                  ST   S Store at S (sign)                              
  132.                  BU*+6  Branch unconditionally around next 5           
  133.                  LDNEG1 Load -1                                        
  134.                  ST   S Store at S                                     
  135.                  LDZERO Load zero                                      
  136.                  SU   X Subtract X (This makes a negative X positive)  
  137.                  ST   X Store back at X                                
  138.                  LD ONE Load 1                                         
  139.                  ST   I I=1 using index register 2                     
  140.                  LD   X Load X                                         
  141.                  SR0008 Shift it right 8                               
  142.                  ST   Y Store at Y                                     
  143.         LOOP     LD   Y Load Y (Note this is the start of a loop)      
  144.                  MU ATE Multiply by 8                                  
  145.                  ST   Y Store at Y                                     
  146.                  LD   X Load X                                         
  147.                 2SL0000 Shift left by I (Contents of register 2)       
  148.                  SR0008 Shift right 8 (This isolates an octal digit)   
  149.                  AD   Y Add previous Y                                 
  150.                  ST   Y Store back at Y                                
  151.                  LD   I Load I                                         
  152.                  AD ONE Add 1                                          
  153.                  ST   I Store at I                                     
  154.                  SUNINE Subtract 9 to see if done                      
  155.                  BNLOOP If negative repeat loop                        
  156.                  LD   X Load X                                         
  157.                  MU   S Multiply by sign                               
  158.                  ST   X Store at X                                     
  159.                  LD   Y Load Y                                         
  160.                  MU   S Multiply by sign                               
  161.                  ST   Y Store at Y                                     
  162.                  PC   X Print X, Y                                     
  163.                  BUSTRT Branch back to start                           
  164.         STOP     HT0000 Halt
  165.         0001          0 Re-origin to define I
  166.            I          0
  167.         1499          0 Re-origin for constants
  168.         ZERO          0 Zero
  169.          ONE          1 One
  170.            S          0
  171.         NEG1         -1 Negative one
  172.          ATE          8 Eight
  173.         NINE          9 Nine
  174.         0999          0 Re-origin for read area
  175.            X          0
  176.            Y          0
  177.                  ENSTRT End of code and starting location
  178.         
  179.         Figure IV-1
  180.         -----------------------------------------------------------------
  181.                                       IV-3
  182.  
  183.  
  184.         The following flow chart is a "bubble" sort.  You enter 10 values 
  185.         (using two reads) then the program makes multiple passes through the 
  186.         data exchanging values where needed until a pass is completed with 
  187.         no exchanges.  The sorted values are then printed.
  188.                          |
  189.                          v
  190.           --------------------------------
  191.          / Read A(0),A(1),A(2),A(3),A(4) /
  192.          --------------------------------
  193.                          |
  194.                          v
  195.           --------------------------------
  196.          / Read A(5),A(6),A(7),A(8),A(9) /
  197.          -------------------------------- 
  198.                          |
  199.                          v
  200.                       +-----+
  201.                       | I=0 |<---------------+
  202.                       | F=0 |                |
  203.                       | J=1 |                |
  204.                       +-----+                |
  205.                          |                   |
  206.                          v                   |
  207.                         / \                  |
  208.                        /   \                 |
  209.                       /     \ <=             |
  210.                 +---><A(I):  >-----+         |
  211.                 |     \ A(J)/      |         |
  212.                 |      \   /       |         |
  213.                 |       \ /        |         |
  214.                 |        | >       |         |
  215.                 |        v         |         |
  216.                 | +-------------+  |         |
  217.                 | |    T=A(I)   |  |         |         +-------+               
  218.                 | | A(I)=A(J)   |  |         |         | STOP  |          
  219.                 | | A(J)=T      |  |         |         +-------+          
  220.                 | +-------------+  |         |             ^              
  221.                 |        |         |         |             |              
  222.                 |        v         |         |   -------------------------
  223.                 |     +-----+      |         |  / Print                  /     
  224.                 |     | F=1 |      |         | /A(5),A(6),A(7),A(8),A(9)/      
  225.                 |     +-----+      |         | -------------------------       
  226.                 |        |         |         |             ^                   
  227.                 |        v         |         |             |                   
  228.                 |    +-------+     |         |             |
  229.                 |    | J=J+1 |<----+         |   -------------------------
  230.                 |    | I=I+1 |               |  / Print                  /
  231.                 |    +-------+               | /A(0),A(1),A(2),A(3),A(4)/ 
  232.                 |        |                   | -------------------------  
  233.                 |        v             not = |             ^              
  234.                 |     < / \ >=              / \ =          |              
  235.                 +------<I:9>--------------><F:0>-----------+              
  236.                         \./                 \./                           
  237.         
  238.                                  Exercise IV-1
  239.         
  240.         
  241.                                       IV-4
  242.  
  243.  
  244.         Experiment with exercise IV-1.  For example, you do not really need 
  245.         to have an explicit J.  It is always equal to I+1.  You can elimi-
  246.         nate it in the following manner.  When you reserve space for A, you 
  247.         can give the location following A(0) a different name, say A1.  Then 
  248.         everyplace that calls for A(J), you can use A1(I).  This technique 
  249.         can be used to facilitate the second read and the second print.  
  250.         That is, give the memory location that corresponds to A(5) a name 
  251.         such as A5.  Then you can say RD  A5 and PC  A5 in order to read and 
  252.         print the second set of A's.
  253.         
  254.                                                   |
  255.                                                   v
  256.                                              +---------+
  257.                                              | T(0)=3  |
  258.                                              | X=5     |
  259.                                              | J=1     |
  260.                                              +---------+
  261.                                                   |
  262.                      +-------+                    v
  263.                      | STOP  |                ----------
  264.                      +-------+               / Read XF /
  265.                          ^                   ----------
  266.                          | >                      |
  267.                         / \                       v
  268.                        /   \ <=                +-----+
  269.                       <X:XF >----------------->| I=0 |<---------+
  270.                        \   /                   +-----+          |
  271.                         \./                       |             |
  272.                          ^                        v             |
  273.                          |                 +------------+       |
  274.                      +-------+             | Q=X/T(I)   |       |
  275.                      | X=X+2 |<------+     | R=X-Q*T(I) |       |
  276.                      +-------+       |     +------------+       |
  277.                          ^           |            |             |
  278.                          |           |            v             |
  279.                      ----------      |         = / \            |
  280.                     / Print X /      +----------<R:0>           |
  281.                     ----------                   \./            |
  282.                          ^                        | Not =       |
  283.                          |                        v             |
  284.                          |                    ---------         |
  285.                      +-------+  +--------+ < /         \ >= +-------+
  286.                      | J=J+1 |<-| T(J)=X |<-<X:T(I)*T(I)>---| I=I+1 |
  287.                      +-------+  +--------+   \         /    +-------+
  288.                                               ---------
  289.         
  290.                                  Exercise IV-2
  291.         
  292.         In exercise IV-2 you will build and display a table of prime numbers 
  293.         (i.e. numbers which are divisible only by themselves and one.)  In 
  294.         the first box of the flow chart, certain quantities are initialized.  
  295.         You may either do this within the executable program or, you may 
  296.         initialize them by entering their initial values when you reserve 
  297.         memory space for them.
  298.         
  299.         
  300.         
  301.                                       IV-5
  302.  
  303.         
  304.         The program requests a final value.  This provides an upper limit 
  305.         for the execution.  Should you wish to terminate earlier, you may 
  306.         abort the program by using ^C (control-C).
  307.         
  308.         The program starts with only the value, 3, in the table (even though 
  309.         2 is also a prime, it is the only even prime and therefore we will 
  310.         only test the odd numbers.)  Starting with a value of 5, each odd 
  311.         number is divided by each value in the table whose square does not 
  312.         exceed the number being tested to see if there is a remainder.  If 
  313.         there is no remainder, the number is not a prime and the program 
  314.         advances to the next number to be tested.  If there is a remainder 
  315.         for all tests made, the number is prime and the value is added to 
  316.         the table for possible later use and the next odd number is tested, 
  317.         etc.
  318.         
  319.         Code this exercise in TRAP, assemble it, and run it.  Note that you 
  320.         do not have to actually store the values, Q and R.  Try this varia-
  321.         tion and other variations you might think of to gain a better under-
  322.         standing of the use of an assembler and machine code.
  323.         
  324.         As further exercises, re-code Figures II-5 and II-8 in TRAP.  Assem-
  325.         ble and execute them.
  326.         
  327.         
  328.         
  329.         
  330.         
  331.         
  332.         
  333.         
  334.         
  335.         
  336.         
  337.         
  338.         
  339.         
  340.         
  341.         
  342.         
  343.         
  344.         
  345.         
  346.         
  347.         
  348.         
  349.         
  350.         
  351.         
  352.         
  353.         
  354.         
  355.         
  356.         
  357.         
  358.         We hope you enjoyed learning TRAC and TRAP.
  359.         
  360.                                       IV-6
  361.